﻿//Write a method that adds two positive integer numbers represented as arrays of digits 
//(each array element arr[i] contains a digit; the last digit is kept in arr[0]). 
//Each of the numbers that will be added could have up to 10 000 digits.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _08AddingNumbersAsArrays
{
    class Program
    {
        static int[] SumNumbersAsArrays(int[] fNumber, int[] sNumber)
        { 
            int[] arraySum = new int[fNumber.Length];
            int sum;
            int carry = 0;
            int value;
            for (int i = fNumber.Length - 1; i >= 1; i--)
			{
                sum = fNumber[i] + sNumber[i];
                value = sum % 10;
                carry = sum/10;
                arraySum[i] = value;
                fNumber[i - 1] += carry;
                if (i == 1)
                {
                    arraySum[i - 1] = fNumber[i - 1] + sNumber[i - 1];
                }
			}
            return arraySum;
        }
        static void Main(string[] args)
        {
            Console.Write("Array size:");
            int arrayOneSize = int.Parse(Console.ReadLine());
            int[] arrayOne = new int[arrayOneSize];
            for (int i = 0; i < arrayOne.Length; i++)
            {
                Console.Write("arrayOne[{0}]:", i);
                arrayOne[i] = int.Parse(Console.ReadLine());
            }

            int[] arrayTwo = new int[arrayOneSize];
            for (int i = 0; i < arrayTwo.Length; i++)
            {
                Console.Write("arrayTwo[{0}]:", i);
                arrayTwo[i] = int.Parse(Console.ReadLine());
            }

            // Here is a test with 10 000 digits. Just because the question said up to 10 000 it was worth to test. Works fine!
            
            //int[] arrayOne = new int[10000];
            //int[] arrayTwo = new int[10000];
            //for (int i = 0; i < arrayOne.Length; i++)
            //{
            //    arrayOne[i] = 6;
            //    arrayTwo[i] = 6;
            //}

            int[] arrayPrint = new int[arrayOne.Length];
            arrayPrint = SumNumbersAsArrays(arrayOne, arrayTwo);
            foreach (var item in arrayPrint)
            {
                Console.Write(item);
            }
            Console.WriteLine();
        }
    }
}
